package com.threez.core.domain;

import com.threez.core.constant.Constants;
import com.threez.core.exception.enums.BaseExceptionEnums;
import com.threez.core.exception.enums.GlobalExceptionEnum;
import com.threez.core.utils.StringUtils;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.Optional;

/**
 * 响应信息主体
 *
 * @author Lion Li
 */
@Data
@NoArgsConstructor
public class R<T> implements Serializable {
    private static final long serialVersionUID = 1L;

    /**
     * 成功
     */
    public static final String SUCCESS = Constants.SUCCESS;

    /**
     * 失败
     */
    public static final String FAIL = Constants.FAIL;

    /**
     * 消息状态码
     */
    private String code;

    /**
     * 消息内容
     */
    private String msg;

    /**
     * 数据对象
     */
    private T data;

    public static <T> R<T> ok() {
        return restResult(null, SUCCESS, "操作成功");
    }

    public static <T> R<T> ok(T data) {
        return restResult(data, SUCCESS, "操作成功");
    }

    public static <T> R<T> ok(String msg) {
        return restResult(null, SUCCESS, msg);
    }

    public static <T> R<T> ok(String msg, T data) {
        return restResult(data, SUCCESS, msg);
    }

    public static <T> R<T> fail() {
        return restResult(null, FAIL, "操作失败");
    }

    @Deprecated
    public static <T> R<T> fail(String msg) {
        return restResult(null, FAIL, msg);
    }

    public static <T> R<T> fail(T data) {
        return restResult(data, FAIL, "操作失败");
    }

    public static <T> R<T> fail(String msg, T data) {
        return restResult(data, FAIL, msg);
    }

    public static <T> R<T> fail(String code, String msg) {
        return restResult(null, code, msg);
    }
    /**
     * 操作失败，存放响应体信息
     *
     * @param globalExceptionEnum 响应体
     * @param <T>
     * @return
     */
    public static <T> R<T> fail(GlobalExceptionEnum globalExceptionEnum) {
        return failEnum(globalExceptionEnum);
    }

    /**
     * 描述信息覆盖替换原则，依据baseExceptionEnums类中的描述进行模板替换
     * @param baseExceptionEnums 指定异常类型
     * @param des  报错信息优化
     * @param <T>
     * @return
     */
    public static <T> R<T> failEnum(BaseExceptionEnums baseExceptionEnums, String... des) {
        String code =  baseExceptionEnums.getCode();
        String msg = Optional.ofNullable(des)
                .map(item -> StringUtils.format(baseExceptionEnums.getMsg(), item))
                .orElseGet(() -> baseExceptionEnums.getMsg());
        return restResult(null, code, msg);
    }

    /**
     * 指定操作失败类型，存放响应体信息
     *
     * @param data 响应体
     * @param <T>
     * @return
     */
    public static <T> R<T> failEnum(T data, BaseExceptionEnums baseExceptionEnums) {
        return restResult(data, baseExceptionEnums.getCode(), baseExceptionEnums.getMsg());
    }



    private static <T> R<T> restResult(T data, String code, String msg) {
        R<T> r = new R<>();
        r.setCode(code);
        r.setData(data);
        r.setMsg(msg);
        return r;
    }

    public static <T> Boolean isError(R<T> ret) {
        return !isSuccess(ret);
    }

    public static <T> Boolean isSuccess(R<T> ret) {
        return R.SUCCESS == ret.getCode();
    }

}
